<template>
  <section>
    <el-card shadow="never">
      <div slot="header">
        个人设置
      </div>
      <div class="columns">
        <div class="column is-full">
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="基础信息" name="first">
              <el-form :label-position="labelPosition" label-width="100px" :model="user">
                <el-form-item label="账号">
                  <el-input v-model="user.username" disabled/>
                </el-form-item>
                <el-form-item label="昵称">
                  <el-input v-model="user.alias"/>
                </el-form-item>
                <el-form-item label="简介">
                  <el-input v-model="user.bio"/>
                </el-form-item>
                <el-form-item>
                  <el-button @click="editAvatar(1)">修改</el-button>
                  <el-button @click="editAvatar(11)">重置</el-button>
                </el-form-item>
                <!--                <el-button type="primary" @click="submitOne">提交</el-button>-->

              </el-form>
            </el-tab-pane>
            <el-tab-pane label="头像" name="second">
              <figure class="image is-48x48">
                <img :src="user.avatar">
              </figure>
              <el-upload
                  class="avatar-uploader"
                  action="http://localhost:8000/api/upload"
                  :headers="{'Authorization' :token}"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
              <el-button @click="editAvatar">修改</el-button>


            </el-tab-pane>
            <el-tab-pane label="电子邮箱" name="third">
              <el-form ref="dynamicValidateForm" :model="user" label-width="100px" class="demo-dynamic">
                <el-form-item
                    prop="email"
                    label="邮箱"
                    :rules="[
                    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                    { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
                  ]"
                >
                  <el-input v-model="user.email"/>
                </el-form-item>

                <el-form-item>
                  <el-button type="primary" @click="editAvatar(2)">提交</el-button>
                  <el-button @click="editAvatar(22)">重置</el-button>
                </el-form-item>
              </el-form>
            </el-tab-pane>
          </el-tabs>
        </div>
      </div>
    </el-card>
  </section>
</template>

<script>
import {getInfo, update, updateAvatar, updateUser} from '@/api/user'

export default {
  name: 'Setting',
  data() {
    return {
      token: localStorage.getItem('Authorization'),
      imageUrl: '',
      fileName: '',
      activeName: 'first',
      labelPosition: 'right',
      user: {
        id: '',
        username: '',
        alias: '',
        bio: '',
        email: '',
        mobile: '',
        avatar: ''
      }
    }
  },

  created() {
    this.fetchInfo()
  },
  methods: {
    editAvatar(count) {
      if (count === 1) {
        let user = {
          alias: this.user.alias,
          bio: this.user.bio
        }
        updateUser(user).then(res => {
          this.$message.success('信息修改成功')
          this.fetchInfo()
        })
        return;
      }
      if(count===11){
        this.user.alias=''
        this.user.bio=''
        return;
      }
      if(count===22){
        this.user.email=''
        return;
      }
      if (count === 2) {
        let user = {
          email: this.user.email
        }
        updateUser(user).then(res => {
          this.$message.success('信息修改成功')
          this.fetchInfo()
        })
        return;
      }
      updateAvatar(this.fileName).then(res => {
        this.$message.success("修改成功！")
      })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.fileName = res.data.fileName
    },

    beforeAvatarUpload(file) {

    }
  },
  fetchInfo() {
    getInfo(this.$route.params.username).then(res => {
      console.log(res)
      const {data} = res
      this.user = data
    })
  },
  handleClick(tab, event) {
    console.log(tab, event)
  },
  submitOne() {
    let user = {
      alias: this.user.alias,
      bio: this.user.bio
    }
    updateUser(user).then(res => {
      this.$message.success('信息修改成功')
      this.fetchInfo()
    })
  },
  resetOne() {
    this.user.alias = ""
    this.user.bio = ""
  },
  submitForm() {
    console.log(this.user)

    update(this.user).then(res => {
      this.$message.success('信息修改成功')
      this.fetchInfo()
    })
  },
  resetForm(formName) {
    this.$refs[formName].resetFields()
  }

}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
